package com.clps.digital.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import com.clps.digital.dao.UserDao;
import com.clps.digital.model.entity.User;
import com.clps.myssm.basedao.BaseDAO;

/**
 * @author: Chase
 * @date: 2024/8/30 8:57
 * @Version: 1.0
 * @description:
 */

public class UserDaoImpl extends BaseDAO<User> implements UserDao {

    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public Long insertUser(User user) {
        return insertOrUpdate("INSERT INTO dbo.[user] ([userAccount], [userPassword]) VALUES (?, ?)", user.getUserAccount(), user.getUserPassword());
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @Override
    public User getUser(Long id) {
        return selectOne("SELECT * from dbo.[user] where [id] = ?", id);
    }

    /**
     * 登录
     * @param userAccount
     * @param userPassword
     * @return
     */
    @Override
    public User login(String userAccount, String userPassword) {
        User user = selectOne("SELECT * from dbo.[user] where [userAccount] = ? AND [userPassword] = ?", userAccount, userPassword);
        if (ObjectUtil.isEmpty(user)) {
            return null;
        }
        return user;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public Boolean updateUser(User user) {
        Long result = insertOrUpdate("UPDATE dbo.[user] SET [userEmail] = ?, [userPhone] = ?, [userName] = ?, [userAvatar] = ?, [userProfile] = ? WHERE [id] = ?",
                user.getUserEmail(), user.getUserPhone(), user.getUserName(), user.getUserAvatar(), user.getUserProfile(), user.getId());
        return result > 0 ? true : false;
    }

    /*
    * @Author 小炎子
    * @Description 保存用户的余额
    * @Date 下午2:42 2024/9/2
    */
    @Override
    public void saveUser(User user) {
        insertOrUpdate("UPDATE dbo.[user] SET [userBalance] = ? where [id] = ?", user.getUserBalance(), user.getId());
    }

}
